package com.atguigu.eduservice.service.Impl;

import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.oss.QinNiuDirectoryUploadUtil;
import com.atguigu.eduservice.service.OssService;
import io.swagger.models.auth.In;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
@Service
public class OssServiceImpl implements OssService {
	@Override
	public String upload(MultipartFile imgFile) {
		/**
		 * 将文件名返回给前端，前端之后展示图片直接向七牛云服务器发送请求
		 */
		String originalFilename = imgFile.getOriginalFilename();
		int index = originalFilename.lastIndexOf(".");
		String format = originalFilename.substring(index - 1);//.jpg  .png
		String Filename = UUID.randomUUID().toString().replace("-", "") + format;
		String url = "http://rqla2ikqy.hn-bkt.clouddn.com/" + Filename;

		try {
			//将图片文件上传到七牛云
			byte[] bytes = imgFile.getBytes();
			QinNiuDirectoryUploadUtil.upload2Qiniu(bytes, Filename);
			//同时将图片的名称上传到redis数据库

		} catch (IOException e) {
			e.printStackTrace();
			return url;
		}

		return  "";

	}

}
